package com.caoyuqi.picture.controll;

import com.caoyuqi.picture.model.Title;
import com.caoyuqi.picture.model.TitleUser;
import com.caoyuqi.picture.model.User;
import com.caoyuqi.picture.service.TitleService;
import com.caoyuqi.picture.service.TitleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.util.List;

@Controller
public class TitleControl {
    public static int title_id = 1;
    @Autowired
    TitleUserService titleUserService;
    @Autowired
    TitleService titleService;
    @Autowired

    @GetMapping("/title")
    public String titlePage()
    {
        return "title";
    }
    @PostMapping("/title")
    public String beginTitle(String titleusername, Model model, HttpSession session)
    {
        TitleUser titleUser = titleUserService.queryByRealname(titleusername);
        session.setAttribute("titleusername",titleusername);
        if(titleUser!=null)
        {
            model.addAttribute("msg","每个人只有一次作答机会");
            return "IndexPage";
        }else {
            String username = (String) session.getAttribute("username");
            if(username == null)
            {
                model.addAttribute("msg","请先登录再作答");
                return "IndexPage";
            }
            titleUserService.InsertInToUser(titleusername,username);
            model.addAttribute("title_id",title_id);
            List<Title> titles = titleUserService.QueryTitleById();
            if(titles!=null)
            {
                model.addAttribute("titles",titles);
            }
            return "title";
        }
    }
    @PostMapping("/result")
    public String ResultPage(HttpServletRequest request,Model model,HttpSession session)
    {
        int grade = 0;
        List<Title> titles = titleService.queryAllTtitle();
        for (int i = 1; i <= titles.size(); i++) {
            String parameter = request.getParameter("" + i);
            if(parameter==null)
            {

                model.addAttribute("titles",titles);
                model.addAttribute("msg","您还有未完成的题目"+i+",请重新认真作答");
                return "title";
            }

        }
        for (int i = 1; i <= titles.size(); i++) {
            String s = titleService.queryTrueById(i);
            String[] parameterValues = request.getParameterValues("" + i);

            if(parameterValues[0].equals(s))
            {
                grade += 10;
            }

        }
        double zhengquelv=grade/(10.0*titles.size());

        DecimalFormat df = new DecimalFormat("#.00");
        String Correctrate = df.format(zhengquelv*100)  + "%";
        String titleusername = (String)session.getAttribute("titleusername");
        titleUserService.getGrade(grade,titleusername);
        titleUserService.updatezql(Correctrate,titleusername);

        List<TitleUser> top4 = titleUserService.queryTop4();
        model.addAttribute("msg","您已交卷");
        model.addAttribute("zhengquelv",Correctrate);

        model.addAttribute("top4",top4);
        model.addAttribute("grade",grade);
        return "END";
    }



}
